﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CUREBusinessLogicLayer;
using CUREBusinessLogicLayer.Harmful;
namespace CURE.Handler
{
    /// <summary>
    /// ConfigHandler 的摘要说明
    /// </summary>
    public class ConfigHandler : IHttpHandler
    {
        ConfigBll configBll = new ConfigBll();
        CUREDataAccessLayers.Common.common comm = new CUREDataAccessLayers.Common.common();
        EventLogBll eventLogBll = new EventLogBll();
        public void ProcessRequest(HttpContext context)
        {
            #region 操作参数
            string action=context.Request.Form["action"];
            string delFalg = context.Request.QueryString["action"];
            string configCode = context.Request.Form["txtConfigCode"];
            string configName = context.Request.Form["txtConfigName"];
            string configValue = context.Request.Form["radConfigValue"];
            string svalue = context.Request.Form["Svalue"];

            #endregion
            #region  登录用户id
            string masterID = string.Empty;
            if (HttpContext.Current.Request.Cookies["MWS_User"] != null)
            {
                masterID = HttpContext.Current.Request.Cookies["MWS_User"]["ID"];
            }
            #endregion
            #region 操作
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "add":
                        bool isExist = configBll.SelectConfigByCodeName(configCode, configName);
                        if (isExist)
                        {
                            context.Response.Write(failure("开关编号或开关名已存在"));
                        }
                        else
                        {
                            bool addSuccess = configBll.InsertConfig(configCode, configName, configValue, svalue);
                            if (addSuccess)
                            {
                                context.Response.Write(success("SystemManagement/ConfigManagement.aspx"));
                                IDictionary<string,object> dic=new Dictionary<string,object>();
                                dic.Add("masterid",masterID);
                                dic.Add("msg", "新增开关：" + configCode + "");
                                eventLogBll.InsertEventLog(dic);
                            }
                            else
                            {
                                context.Response.Write(failure("添加失败"));
                            }
                        }
                        break;
                    case "edit":
                        bool exist = configBll.SelectConfigByCodeAndName(configCode, configName);
                        if (exist)
                        {

                        }
                        else
                        {
                            bool upSuccess = configBll.UpdateConfig(configCode, configName, configValue, svalue);
                            if (upSuccess)
                            {
                                context.Response.Write(success("SystemManagement/ConfigManagement.aspx"));
                                IDictionary<string, object> dic = new Dictionary<string, object>();
                                dic.Add("masterid", masterID);
                                dic.Add("msg", "修改开关：" + configCode + "");
                                eventLogBll.InsertEventLog(dic);
                            }
                            else
                            {
                                context.Response.Write(failure("修改失败"));
                            }
                        }
                        break;
                }
            }
            else
            {
                if (delFalg == "skilllist")
                {
                    if (comm.SwitchIsOn("IsSkillApproval"))
                    {
                        context.Response.Write("1");
                    }
                    else
                    {
                        context.Response.Write("0");
                    }
                }
                else
                {
                    context.Response.Write(failure("无操作指定"));
                }
            }
            #endregion

            
        }
        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}